<!DOCTYPE html>
<html>
	<head>
		<title>设置个人资料</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/plugins/bootstrap-4.1.1-dist/css/bootstrap.min.css">
		<script src="/js/plugins/jquery/jquery.min.js"></script>
		<script src="/js/plugins/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/myInfo.css" />
		<script src="/js/plugins/jrender/jrender.min.js"></script>
		<script src="/js/plugins/jquery-form/jquery.form.js"></script>

		<script>
            $(function () {
                //获取当前邓禄普用户信息
                var user = JSON.parse(sessionStorage.getItem("user"))
                //渲染数据
                $(".info").renderValues(user)

				//设置背景图回显,通过jquery的方式设置
				//user.coverImgUrl
				$(".myBg").css('background-image','url("'+user.coverImgUrl+'")')


				//标记当前是修改背景还是修改头像
				var flag = true;

				//修改头像按钮
				$("#headBtn").click(function () {
					//触发文件上传框的点击动作,点击那个隐藏的框
					//有参数,绑定动作,传入回调函数
					//没有参数(没有回调函数),触发文件上传框的点击动作,
					$("#imgInput").click()

					//设置true是修改头像
					flag = true;

                })

				//修改背景按钮
                $("#coverBtn").click(function () {
                    //触发文件上传框的点击动作,点击那个隐藏的框
                    //有参数,绑定动作,传入回调函数
                    //没有参数(没有回调函数),触发文件上传框的点击动作,
                    $("#imgInput").click()

                    //设置fales是修改头像
                    flag = false;
                })


				//如果文件上传框发生改变,就可以提交表单
				//找到文件上传框,调用change(状态变更的函数)
				$("#imgInput").change(function () {
				    //检查有值时(有上传文件),才提交
					if($(this).val()){
						$("#imgForm").ajaxSubmit(function (data) {
							if(data.status == 1){
							    if(flag){
									//把新图片的url回显到img
									$("#headImg").attr('src',data.url)
									//设置头像隐藏域的值
									$("[name=headImgUrl]").val(data.url)
								}else{
							        //把新图片的url回显到背景
									$(".myBg").css('background-image','url("'+data.url+'")')
									//设置背景隐藏域的值
                                    $("[name=coverImgUrl]").val(data.url)
								}
							}
                        })
					}
                })

				//保存按钮的提交
				$("#saveBtn").click(function () {
					$("#infoForm").ajaxSubmit(function (data) {
						if(data.success){

						    //回显时,要把存在缓存的json对象拿出来,再转回字符串
                            sessionStorage.setItem("user",JSON.stringify(data.result))
							//完成上面操作,再跳转
						    window.location.href = "/mine/profiles.html"
						}
                    })
                })
            })
		</script>
		
	</head>

	<body>

		<div class="search-head">
			<div class="row nav-search">
				<div class="col">
					<a href="javascript:window.history.go(-1);">
						<span>取消</span>
					</a>
				</div>
				<div class="col">
					<span>设置个人资料</span>
				</div>
				<div class="col" >
					<a href="#" id="saveBtn" >
						<span>保存</span>
					</a>
				</div>
			</div>
		</div>

		<!--action="/images"  资源接口,关联对应的controller-->
		<form action="/images" id="imgForm" method="POST">

		<!--style="display: none"   不显示上传框,点击才出现-->
		<!--type="file  上传框,不能改-->
		<input type="file" name="file" style="display: none" id="imgInput">
		</form>

		<div class="container info">
			<div class="myBg">
				<img id="headImg"  class="rounded-circle" render-src="headImgUrl"/>
				<div class="editHead" id="headBtn" ><i class="fa fa-pencil-square fa-lg " aria-hidden="true"></i></div>
				<button  class="btn btn-outline-light ibtn " id="coverBtn">设置背景</button>
			</div>

			<form id="infoForm" method="post" action="/users">
					<input type="hidden" name="_method" value="put">
					<input type="hidden" name="id" render-value="id">
					<input type="hidden" name="headImgUrl" render-value="headImgUrl">
					<input type="hidden" name="coverImgUrl" render-value="coverImgUrl">
					<input type="hidden" name="email" render-value="email">

					<label>昵称</label><input type="text" name="nickName" class="form-control" render-value="nickName">
					<label>性别</label><select class="form-control" name="gender" render-value="gender">
							<option value="-1">保密</option>
							<option value="1">男</option>
							<option value="0">女</option>
						</select>
					<label>地区</label><input type="text" name="place"  class="form-control" render-value="place">
					<label>签名</label><textarea name="sign" class="form-control" rows="5" render-value="sign"></textarea>
			</form>
		</div>
		
		
		
	</body>
</html>
